package cn.tedu.baking.controller;

import cn.tedu.baking.pojo.dto.ContentDTO;
import cn.tedu.baking.pojo.vo.*;
import cn.tedu.baking.response.JsonResult;
import cn.tedu.baking.service.IContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/contents/")
public class ContentController {
    @Autowired
    IContentService service;

    @PostMapping("add-new")
    public JsonResult addNew(@RequestBody ContentDTO contentDTO) {
        System.out.println("contentDTO = " + contentDTO);
        service.addNew(contentDTO);
        return JsonResult.ok();
    }

    /*@GetMapping("{type}/management")
    public JsonResult listForManagementByType(@PathVariable Long type ,
                       @AuthenticationPrincipal CustomUserDetails userDetails){
        System.out.println("type = " + type + ", userDetails = " + userDetails);
        List<ContentManagementVO> list = service.listForManagementByType(type,userDetails.getId());
        return JsonResult.ok(list);
    }*/
    @GetMapping("{type}/{id}/management")
    public JsonResult listForManagementByType(@PathVariable Long type,
                                              @PathVariable Long id) {

        List<ContentManagementVO> list = service.listForManagementByType(type, id);
        return JsonResult.ok(list);
    }

    @PostMapping("{id}/delete")
    public JsonResult delete(@PathVariable Long id) {
        service.deleteById(id);
        return JsonResult.ok();
    }

    @GetMapping("{id}/update")
    public JsonResult selectForUpdateById(@PathVariable Long id) {

        ContentUpdateVO contentUpdateVO = service.selectForUpdateById(id);
        return JsonResult.ok(contentUpdateVO);
    }

    @GetMapping("{type}/{categoryId}/index")
    public JsonResult listForIndex(@PathVariable Long type, @PathVariable Long categoryId) {
        System.out.println("type = " + type + ", categoryId = " + categoryId);
        List<ContentIndexVO> list = service.listForIndex(type, categoryId);
        System.out.println(list);
        return JsonResult.ok(list);
    }

    @GetMapping("{id}/detail")
    public JsonResult getDetailById(@PathVariable Long id) {
        ContentDetailVO contentDetailVO = service.getDetailById(id);
        return JsonResult.ok(contentDetailVO);
    }

    @GetMapping("{id}/other")
    public JsonResult listForOtherByUserId(@PathVariable Long id) {
        List<ContentSimpleVO> list = service.listForOtherByUserId(id);
        return JsonResult.ok(list);
    }

    @GetMapping("hot")
    public JsonResult listForHot() {
        List<ContentSimpleVO> list = service.listForHot();
        return JsonResult.ok(list);
    }

    @GetMapping("{type}/list")
    public JsonResult listByType(@PathVariable Long type) {
        System.out.println("type = " + type);
        List<ContentIndexVO> list = service.listByType(type);
        System.out.println("list = " + list);
        return JsonResult.ok(list);
    }

    @GetMapping("{wd}/search")
    public JsonResult listByWd(@PathVariable String wd) {
        List<ContentIndexVO> list = service.listByWd(wd);
        return JsonResult.ok(list);
    }

    @GetMapping("{type}/list/admin")
    public JsonResult listForAdminByType(@PathVariable Long type) {
        List<ContentAdminVO> list = service.listForAdminByType(type);
        return JsonResult.ok(list);
    }

}
